package me.zhengjie.pm.projectplan.rest;

import me.zhengjie.aop.log.Log;
import me.zhengjie.pm.projectplan.domain.ProjectPlan;
import me.zhengjie.pm.projectplan.service.ProjectPlanService;
import me.zhengjie.pm.projectplan.service.dto.ProjectPlanQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author ZGZ
* @date 2020-04-25
*/
@Api(tags = "项目计划管理")
@RestController
@RequestMapping("/api/projectPlan")
public class ProjectPlanController {

    private final ProjectPlanService projectPlanService;

    public ProjectPlanController(ProjectPlanService projectPlanService) {
        this.projectPlanService = projectPlanService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('projectPlan:list')")
    public void download(HttpServletResponse response, ProjectPlanQueryCriteria criteria) throws IOException {
        projectPlanService.download(projectPlanService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询项目计划")
    @ApiOperation("查询项目计划")
    @PreAuthorize("@el.check('projectPlan:list')")
    public ResponseEntity<Object> getProjectPlans(ProjectPlanQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(projectPlanService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增项目计划")
    @ApiOperation("新增项目计划")
    @PreAuthorize("@el.check('projectPlan:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody ProjectPlan resources){
        return new ResponseEntity<>(projectPlanService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改项目计划")
    @ApiOperation("修改项目计划")
    @PreAuthorize("@el.check('projectPlan:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody ProjectPlan resources){
        projectPlanService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除项目计划")
    @ApiOperation("删除项目计划")
    @PreAuthorize("@el.check('projectPlan:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        projectPlanService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}